<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/9/21
 * Time: 16:17
 */
//
class tool
{
    public static function main($class, $fun, $arg = '')
    {
        $obj = new $class();
        $obj->$fun($arg);
    }
}

class a
{
    public function fa($arg)
    {
        echo $arg;
    }
}

class b
{
    public function fb($arg)
    {
        echo $arg;
    }
}
//钩子的调用
tool::main('a','fa','');

class sendSms
{
    const ONE = 1;
    const TWO = 2;
    private $type = 1;
    private $one = array( ////通道1配置项
        'api_send_url' => 'xxxx',
        'api_balance_query_url' => 'xxxxx',
        'api_account' => 'xxxx',
        'api_password' => 'xxxxx',
    );

    private $two = array(  //通道2配置项
        'api_key' => 'xxxx',
        'api_id' => 'xxxxx',
        'api_send_url' => 'xxxxx',
    );

    public function __construct($type = 1)
    {
        switch ($type) {
            case self::ONE:
                $this->type = $type;
                break;
            case self::TWO:
                $this->type = $type;
                break;
            default:
                $this->type = false;
        }
    }

    /**
     * @param $mobile 手机号
     * @param $msg 发送的信息
     * @User: yfl
     * @Time: 2017年9月21日 16:26:02
     * @description:对外提供的接口
     */
    public function sendMsg($mobile, $msg)
    {
        switch ($this->type) {
            case self::ONE:
                $this->sendByOne($mobile, $msg);
                break;
            case self::TWO:
                $this->sendByTwo($mobile, $msg);
                break;
            default:
                return false;
        }
    }

    /**
     * @param $mobile
     * @param $msg
     * @param string $needstatus
     * @param string $extno
     * @return bool|mixed
     * @User: yfl
     * @Time: 2017年9月21日 16:41:41
     * @description: 采用通道一发送数据
     */
    private function sendByOne($mobile, $msg, $needstatus = 'false', $extno = '')
    {
        $postArr = array(
            'account' => $this->one['api_account'],
            'pswd' => $this->one['api_password'],
            'msg' => $msg, 'mobile' => $mobile,
//            'needstatus' => $needstatus,
//            'extno' => $extno
        );
        $result = $this->_curlPost($this->one['api_balance_query_url'], $postArr);
        $result = self::execResult($result);
        return $result[1] == 0 ? true : $result[1];
    }

    /**
     * @param $mobile
     * @param $msg
     * @User: yfl
     * @Time:2017年9月21日 16:42:07
     * @description:采用通道二发送数据
     */
    private function sendByTwo($mobile, $msg)
    {
        $postArr = array(
            'id' => $this->two['api_id'],
            'key' => $this->two['api_key'],
            'msg' => $msg,
            'mobile' => $mobile,
        );
        $result = $this->_curlPost($this->two['api_send_url'], $postArr);
        $result = static::execResult($result);
        return $result[1] == 0 ? true : $result[1];
    }

    /**
     * @param $result
     * @return array
     * @User: yfl
     * @Time:
     * @description: 处理返回的结果
     */
    private static function execResult($result)
    {
        $result = preg_split("/[,\r\n]/", $result);
        return $result;
    }

    /**
     * @param $url
     * @param array $postFields
     * @return mixed
     * @User: yfl
     * @Time: 2017年9月21日 16:35:06
     * @description: 通过CURL发送HTTP请求 post方式
     */
    private function _curlPost($url, array $postFields)
    {
        $postFields = http_build_query($postFields);
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields);
        $result = curl_exec($ch);
        curl_close($ch);
        return $result;
    }
}